//#include "sqlquerymodel.h"

//SqlQueryModel::SqlQueryModel(QObject *parent) :
//    QSqlQueryModel(parent)
//{
//    this->modelName = "";
//    this->query = "";
//    bindings = QHash<QString, QList<ModelBinding> >();
//}

//void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
//{
//    if(query.length() == 0){
//        this->query = query;
//    }
//    QSqlQueryModel::setQuery(query, db);
//    generateRoleNames();
//    QSqlError le = this->lastError();

//    if (le.isValid()){
//        QString errMsg("Query was:\n%1\n\nError:\n%2");
//        QMessageBox::critical(0, "Query error", errMsg.arg(query).arg(le.text()));
//        return;
//    }
//}

//void SqlQueryModel::setQuery(const QSqlQuery &query)
//{
//    QSqlQueryModel::setQuery(query);
//    generateRoleNames();
//    QSqlError le = this->lastError();
//    if (le.isValid()){
//        QString errMsg("Query was:\n%1\n\nError:\n%2");
//        qDebug() << errMsg.arg(query.lastQuery()).arg(le.text());
//        /* We're not using a MessageBox because it causes a segfault for some reason. */
//        //QMessageBox::critical(0, "Query error", errMsg.arg(query.lastQuery()).arg(le.text()));
//        return;
//    }
//}
///**
// * @brief SqlQueryModel::exec This function prepares and executes the query.
// */
//void SqlQueryModel::exec()
//{
//    qDebug() << "Executing query on model" << this->modelName;
//    /* Let's create a QSqlQuery. It will store the query and we'll bind values to it.*/
//    QSqlQuery sQuery;
//    /* If we initialize the query with the string, then we CANNOT use bind (won't work and won't show any error).*/
//    sQuery.prepare(this->query);

//    /** Now, let's go through all the models associated to this instance.
//     * For each of them, we'll bind its value. Note that we're avoiding making a copy by using the adresses
//     * cf. : http://stackoverflow.com/questions/17106243/qt-iterator-not-accessing-the-correct-object
//    **/

//    QHash<QString, QList<ModelBinding> >::iterator bindingsIt;
//    for (bindingsIt = bindings.begin(); bindingsIt != bindings.end(); bindingsIt++){
//        QList<ModelBinding>::iterator eachBindingIt;
//        QList<ModelBinding>& curBinding = *bindingsIt;

//        for(eachBindingIt = curBinding.begin(); eachBindingIt != curBinding.end(); eachBindingIt++){

//            ModelBinding& binding = *eachBindingIt;
//            binding.bindToQuery(&sQuery);
//        }
//    }

//    /* Let's not forget to execute this query, or nothing will be displayed in the QML. */
//    sQuery.exec();
//    qDebug() << "----------------";
//    qDebug() << sQuery.lastQuery();
//    QMapIterator<QString, QVariant> i(sQuery.boundValues());
//    while (i.hasNext()) {
//        i.next();
//        qDebug() << i.key().toAscii().data() << "="
//                 << i.value().toString().toAscii().data();
//    }
//    qDebug() << "----------------";
//    this->setQuery(sQuery);
//}

//void SqlQueryModel::generateRoleNames()
//{
//    QHash<int, QByteArray> roleNames;
//    for( int i = 0; i < record().count(); i++) {
//        roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toAscii();
//    }

//    qDebug() << "Generating role names for" << modelName;

//    setRoleNames(roleNames);
//}

//void SqlQueryModel::search(QString str){

//    QString nQuery (query);
//    nQuery.append(" WHERE ").append(str);
//    qDebug() << "Set query to: " << nQuery;
//    this->setQuery(nQuery);
//    QSqlError le = this->lastError();

//    if (le.isValid()){
//        QString errMsg("An error occurred while loading the file.\n\nQuery was:\n%1\n\nError:\n%2");
//        QMessageBox::critical(0, "Database error", errMsg.arg(nQuery).arg(le.text()));
//    }
//}

//QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
//{

//    QVariant value = QSqlQueryModel::data(index, role);
//    if(role < Qt::UserRole){
//        value = QSqlQueryModel::data(index, role);
//    }else{
//        int columnIdx = role - Qt::UserRole - 1;
//        QModelIndex modelIndex = this->index(index.row(), columnIdx);
//        value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);

//        qDebug() << modelName << ":" << record().fieldName(columnIdx) << "=" << value;

//        emit bindedValueChanged(modelName, record().fieldName(columnIdx), value);
//    }
//    return value;
//}

//void SqlQueryModel::reset()
//{
//    qDebug() << "Resetting original SQL query to: " << query;
//    this->setQuery(query);
//}

//void SqlQueryModel::updateBindings(QString modelName, QString col, QVariant val)
//{

//    /** Now, let's go through all the models associated to this instance.
//     * We're going to see if the new signal we got is used for this model (for model name and column name).
//     * If so, we'll assigned it and then we'll execute this query by calling exec().
//    **/

//    bool anyValueChanged = false;

//    QHash<QString, QList<ModelBinding> >::iterator bindingsIt;

//    for (bindingsIt = bindings.begin(); bindingsIt != bindings.end(); bindingsIt++){

//        QList<ModelBinding>::iterator eachBindingIt;
//        QList<ModelBinding>& curBinding = *bindingsIt;

//        for(eachBindingIt = curBinding.begin(); eachBindingIt != curBinding.end(); eachBindingIt++){

//            ModelBinding& binding = *eachBindingIt;
//            if(bindingsIt.key() == modelName && binding.column == col){
//                binding.value = val;
//                anyValueChanged = true;
//            }
//        }
//    }

//    if (anyValueChanged){
//        this->exec();
//    }
//}
